/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 *//*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package comparators;

import company.search.interfaces.SearchRequerimetsOR;
import comparators.abstraction.CandidateCapableComparator;
import entities.Candidate;
import interfaces.CandidateCapable;
import interfaces.SearchCapable;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import util.filter.Predicate;

/**
 *
 * @author Damián
 * @param <T>
 * @param <S>
 */
public class CandidateCapableSearchCapableComparator<T extends CandidateCapable, S extends SearchCapable & Predicate<T>> extends CandidateCapableComparator<T,S>{
    private final Integer keywordPoints=150;
    
    public CandidateCapableSearchCapableComparator(S predicate) {
        super(predicate);
    }
    
    @Override
    public int compare(T candidateCapable1,T candidateCapable2){
        Candidate c1=candidateCapable1.getCandidate();
        Candidate c2=candidateCapable2.getCandidate();
        Integer c1Points=0;
        Integer c2Points=0;
        if(getPredicate().getSearch().getKeywords()!=null && !getPredicate().getSearch().getKeywords().equals(" ")){
            String[] keyWordsSplitted = getPredicate().getSearch().getKeywords().split(" "); 
            List<String> keyWords=new ArrayList<String>();
            for ( String keyword : keyWordsSplitted) {
                if(!keyword.trim().equals("de") && !keyword.trim().equals("con") && !keyword.trim().equals("y")){
                    keyWords.add(Normalizer.normalize(keyword.trim(), Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", ""));
                }
            }
            Integer eachPoint=keywordPoints/keyWords.size();
            for (String keyword : keyWords) {
                Integer keywordPointsIteration=0;
                Pattern p = Pattern.compile(Pattern.quote(keyword),Pattern.CASE_INSENSITIVE);
                Matcher m = p.matcher(Normalizer.normalize(c1.getSearcheableKeywordString().trim(), Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", ""));
                while(m.find()){
                    keywordPointsIteration+=eachPoint;
                }
                if(keywordPointsIteration>eachPoint){
                    c1Points+=Math.min(keywordPointsIteration, eachPoint*2);
                }
                else{
                    c1Points+=keywordPointsIteration;
                }
                keywordPointsIteration=0;
                m=p.matcher(Normalizer.normalize(c2.getSearcheableKeywordString().trim(), Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", ""));
                while(m.find()){
                    keywordPointsIteration+=eachPoint;
                }
                if(keywordPointsIteration>eachPoint){
                    c2Points+=Math.min(keywordPointsIteration, eachPoint*2);
                }
                else{
                    c2Points+=keywordPointsIteration;
                }
            }           
        }
               
        for(SearchRequerimetsOR searchRequerimetsOR: getPredicate().getSearch().getRequerimentsOR()){
            c1Points+=searchRequerimetsOR.getCandidateTotalPoints(c1);
            c2Points+=searchRequerimetsOR.getCandidateTotalPoints(c2);
        }
        int res= c2Points.compareTo(c1Points);
        if(res==0){
            res=c1.compareTo(c2);
        }
        return res;
    }
}
